package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjBill;
import com.moji.entity.MjBillAddress;
import com.moji.entity.MjOrder;
import com.moji.entity.MjProduct;
import com.moji.entity.dto.QueryBillAddressDto;
import com.moji.entity.dto.QueryBillDto;
import com.moji.mapper.MjBillMapper;
import com.moji.service.MjBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.service.MjOrderService;
import com.moji.service.MjProductService;
import com.moji.service.MjWorkOrderService;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 发票表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-08-21
 */
@Service
public class MjBillServiceImpl extends ServiceImpl<MjBillMapper, MjBill> implements MjBillService {


    @Resource
    private MjOrderService mjOrderService;

    @Resource
    private MjProductService mjProductService;

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjBill> queryByPage(QueryBillDto dto) {
        LambdaQueryWrapper<MjBill> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(dto.getCompanyName()),MjBill::getCompanyName
                ,dto.getCompanyName());
        wrapper.eq(MjBill::getCreateId,MoJiCommonUtils.getUserCode());
        //创建时间倒叙
        wrapper.orderByDesc(MjBill::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjBill> mjBillList = list(wrapper);
        PageInfo<MjBill> info = new PageInfo<>(mjBillList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(List<Long>  ids) {
        if(ObjectUtil.isEmpty(ids)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        ids.forEach(one->{
            MjBill mjBill=this.getById(one);
            if(mjBill==null){
                throw new RuntimeException("主键ID不能为空,请核查!");
            }
            //更新订单未开票
            MjOrder mjOrder=mjOrderService.getOne(new LambdaQueryWrapper<MjOrder>()
                    .eq(MjOrder::getOrderCode,mjBill.getOrderCode()));
            if(mjOrder !=null){
                mjOrder.setBillStatus(0);
                mjOrderService.updateById(mjOrder);
            }
            this.removeById(one);
        });
        return true;
    }

    @Override
    public boolean add(MjBill mjBill) {
        //编码自动生成
        String code = idGenerator.idGenerator("FB");
        mjBill.setBillCode(code);
        mjBill.setCreateId(MoJiCommonUtils.getUserCode());
        mjBill.setUpdateId(MoJiCommonUtils.getUserCode());
        mjBill.setCreateTime(new Date());
        mjBill.setUpdateTime(new Date());

        //更新订单开票中
        MjOrder mjOrder=mjOrderService.getOne(new LambdaQueryWrapper<MjOrder>()
        .eq(MjOrder::getOrderCode,mjBill.getOrderCode()));
        if(mjOrder !=null){
            mjOrder.setBillStatus(1);
            mjOrderService.updateById(mjOrder);
        }
        mjBill.setOrderName(mjOrder.getOrderName());
        mjBill.setPayAmount(mjOrder.getPayAmount());
        mjBill.setPayType(mjOrder.getPayType());

        MjProduct mjProduct=mjProductService.getOne(new LambdaQueryWrapper<MjProduct>()
                .eq(MjProduct::getId,mjOrder.getProductCode()));
        mjBill.setProductCode(mjProduct.getId()+"");
        mjBill.setProductName(mjProduct.getProductName()+"");
        return this.save(mjBill);
    }

    @Override
    public boolean edit(MjBill mjBill) {
        MjBill mjBill1=this.getById(mjBill.getId());
        if(mjBill1 ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        BeanUtils.copyProperties(mjBill,mjBill1);
        mjBill1.setUpdateId(MoJiCommonUtils.getUserCode());
        mjBill1.setUpdateTime(new Date());
        return this.updateById(mjBill1);
    }

    @Override
    public MjBill detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }
}
